<template>
  <div class="centerPage">
    <headerPage></headerPage>
    <div class="centerPage-content">
      <el-card class="profile-card" :body-style="{ padding: '0px' }">
        <div class="profile-header">
          <div class="avatar-container">
            <el-image 
              class="avatar-image"
              :src="user.avatar"  
              :preview-src-list="[user.avatar]">
            </el-image>
            <div class="avatar-actions">
              <el-upload
                ref="upload"
                :action="uploadAvatarUrl()+ '/'+ this.user.id"
                :show-file-list="false"
                :before-upload="beforeAvatorUpload"
                :on-success="handleAvatorSuccess"
                accept="image/*"
                >
                <el-button type="text" size="small" icon="el-icon-camera">更换头像</el-button>
              </el-upload>
            </div>
          </div>
          <div class="profile-title">
            <h2>{{user.userName || '未设置用户名'}}</h2>
            <p class="profile-subtitle">{{user.email || '未设置邮箱'}}</p>
          </div>
        </div>
        
        <div class="profile-content">
          <el-form 
            :model="user" 
            :rules="rules" 
            ref="ruleForm" 
            label-width="100px"
            class="profile-form"
            size="medium">
            
            <el-form-item label="登录账号" prop="loginAccount">
              <el-input disabled v-model="user.loginAccount"></el-input>
            </el-form-item>
            
            <el-form-item label="用户名" prop="userName">
              <el-input v-model="user.userName" placeholder="请输入用户名"></el-input>
            </el-form-item>
            
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="user.email" placeholder="请输入邮箱"></el-input>
            </el-form-item>
            
            <el-form-item label="联系电话" prop="tel">
              <el-input v-model="user.tel" placeholder="请输入手机号"></el-input>
            </el-form-item>
            
            <el-form-item label="性别">
              <el-radio-group v-model="user.sex">
                <el-radio label="0">男</el-radio>
                <el-radio label="1">女</el-radio>
              </el-radio-group>
            </el-form-item>
            
            <el-form-item label="密码更新" prop="pwdUpdateDate">
              <div class="password-section">
                <span class="password-date">{{user.pwdUpdateDate}}</span>
                <el-button 
                  type="text" 
                  icon="el-icon-key"
                  @click="changePassword">
                  修改密码
                </el-button>
              </div>
            </el-form-item>

            <el-form-item class="form-actions">
              <el-button type="primary" @click="submit">保存修改</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-card>
    </div>
    
    <el-dialog
      title="修改密码"
      :visible.sync="passwordDialogVisible"
      width="400px"
      custom-class="password-dialog"
      :before-close="handlePasswordClose">
      <el-form class="password-form" size="medium">
        <el-form-item label="当前密码">
          <el-input 
            v-model="oldPassword" 
            show-password 
            placeholder="请输入当前密码">
          </el-input>
        </el-form-item>
        <el-form-item label="新密码">
          <el-input 
            v-model="newPassword" 
            show-password 
            placeholder="请输入新密码">
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handlePasswordClose">取 消</el-button>
        <el-button type="primary" @click="passwordSubmit">确 定</el-button>
      </span>
    </el-dialog>
    
    <bottomPage></bottomPage>
  </div>
</template>

<script>
  import {mixin} from "../../minix";
  import headerPage from "../../components/header"
  import bottomPage from "../../components/bottom"
  import {getUser,setUserInfo,setUserAvatar,changePassword} from '../../api/api' 
  export default {
    mixins: [mixin],
    data() {
      var checkPhone = (rule, value, callback) => {
          if (!value) {
              return callback(new Error('请输入联系电话'));
          } else {
              const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
              if (reg.test(value)) {
                  callback();
              } else {
                  return callback(new Error('请输入正确的联系电话'));
              }
          }
      };
      return{
        school: [],
        major: [],
        user: {},
        avatar: [],
        rules: {
          userName: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
          ],
          email: [
            { required: true, message: '请输入邮箱地址', trigger: 'blur' },
            { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
          ],
          tel: [
            { required: true, validator:checkPhone, message: '请输入正确的联系电话', trigger: 'blur' },
          ],
        },
        oldPassword: "",
        newPassword: "",
        passwordDialogVisible: false,
      }
    },
    components: {
      headerPage,
      bottomPage
    },
    methods: {
      handlePasswordClose() {
        this.$bus.$emit('password', false)
      },
      passwordSubmit() {
        var param = {
          id: this.user.id,
          password: this.oldPassword,
          newPassword: this.newPassword
        }
        changePassword(param).then(res => {
          if(res.code == 1000) {
            this.$notify.success({
              title: '成功',
              message: "密码修改成功"
            });
            this.$bus.$emit('password', false)
          } else {
            this.$notify.error({
              title: '错误',
              message: res.message
            });
          }
        })
      },
      submit() {
        this.$refs["ruleForm"].validate((valid) => {
          if (valid) {
            setUserInfo(this.user).then(res => {
              if(res.code == 1000) {
                this.$message({
                  type: 'success',
                  message: '保存成功!'
                });
                this.getUserInfo()
              } else {
                this.$notify.error({
                  title: '错误',
                  message: res.message
                });
              }
            })
          } else {
            return false;
          }
        });
      },
      getUserInfo() {
        getUser().then(res => {
            if(res.code == 1000) {
              this.user = res.data
              this.user.sex = res.data.sex + ""
              // 直接使用返回的完整头像URL
              this.user.avatar = res.data.avatar
              // 更新预览图片列表
              this.avatar = [res.data.avatar]
              // 更新本地存储
              window.localStorage.setItem("user_info", JSON.stringify(res.data))
              // 通知Vuex更新用户信息
              this.$store.commit('setUser', JSON.stringify(res.data))
            } else {
              this.$notify.error({
                title: '错误',
                message: res.message
              });
            }
        })
      },
      changePassword() {
        //修改密码
        this.$bus.$emit('password', true)
      },
      handleAvatorSuccess(res){
        let _this = this;
        if(res.code == 1000){
            _this.$message({
              type: 'success',
              message: '上传成功!'
            });
            // 更新用户信息
            getUser().then(res => {
                if(res.code == 1000) {
                    // 更新本地存储的用户信息
                    window.localStorage.setItem("user_info", JSON.stringify(res.data));
                    // 更新当前页面的用户信息
                    this.user = res.data;
                    this.user.sex = res.data.sex + "";
                    // 直接使用返回的完整头像URL
                    this.user.avatar = res.data.avatar;
                    // 更新预览图片列表
                    this.avatar = [res.data.avatar];
                    // 通知Vuex更新用户信息
                    this.$store.commit('setUser', JSON.stringify(res.data));
                }
            });
        } else {
            _this.$message({
              type: 'error',
              message: '上传失败!'
            });
        }
      },
    },
    created() {
     
    },
    mounted() {
      // 监听collapse
      this.$bus.$on('password', res=>{
        this.passwordDialogVisible = res
      })
      this.getUserInfo()
    }
 }
</script>

<style scoped>
.centerPage {
  min-height: 100vh;
  background-color: #f5f7fa;
}

.centerPage-content {
  max-width: 900px;
  margin: 0 auto;
  padding: 40px 20px;
}

.profile-card {
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.05);
}

.profile-header {
  background: linear-gradient(135deg, #1890ff 0%, #36cfc9 100%);
  padding: 40px;
  color: white;
  text-align: center;
}

.avatar-container {
  position: relative;
  display: inline-block;
  margin-bottom: 20px;
}

.avatar-image {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  border: 4px solid rgba(255,255,255,0.2);
  overflow: hidden;
  background-color: #fff;
}

.avatar-actions {
  margin-top: 10px;
}

.avatar-actions .el-button {
  color: rgba(255,255,255,0.9);
}

.avatar-actions .el-button:hover {
  color: #fff;
}

.profile-title {
  margin-top: 10px;
}

.profile-title h2 {
  margin: 0;
  font-size: 24px;
  font-weight: 500;
}

.profile-subtitle {
  margin: 10px 0 0;
  opacity: 0.8;
  font-size: 14px;
}

.profile-content {
  padding: 40px;
  background: #fff;
}

.profile-form {
  max-width: 600px;
  margin: 0 auto;
}

.password-section {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.password-date {
  color: #909399;
  font-size: 14px;
}

.form-actions {
  margin-top: 40px;
  text-align: center;
}

/* 密码弹窗样式 */
:deep(.password-dialog) {
  border-radius: 8px;
}

:deep(.password-dialog .el-dialog__header) {
  padding: 20px;
  background: #f8f9fa;
  margin: 0;
}

:deep(.password-dialog .el-dialog__body) {
  padding: 30px;
}

.password-form {
  margin: 0;
}

:deep(.el-form-item__label) {
  font-weight: 500;
  color: #2c3e50;
}

:deep(.el-input__inner) {
  border-radius: 4px;
}

:deep(.el-input__inner:hover) {
  border-color: #1890ff;
}

:deep(.el-radio__input.is-checked .el-radio__inner) {
  background-color: #1890ff;
  border-color: #1890ff;
}

:deep(.el-radio__input.is-checked + .el-radio__label) {
  color: #1890ff;
}

/* 响应式布局 */
@media (max-width: 768px) {
  .centerPage-content {
    padding: 20px;
  }
  
  .profile-header {
    padding: 30px 20px;
  }
  
  .profile-content {
    padding: 30px 20px;
  }
  
  .avatar-image {
    width: 100px;
    height: 100px;
  }
  
  .profile-title h2 {
    font-size: 20px;
  }
}
</style>